package Cryptographic;

import java.security.MessageDigest;

/**
 * 
 * @author Or Stern
 *
 */

public class HashFunction {
	private int outlen;
	private MessageDigest hash;
	
	/**
	 * 
	 * @param hashName the name of the hash function (must be "SHA-256", "SHA-384" or "SHA-512").
	 * @throws Exception the hash name is illegal.
	 */
	public HashFunction(String hashName) throws Exception
	{
		if (hashName.equals("SHA-256") || hashName.equals("SHA-384") || hashName.equals("SHA-512"))
			hash = MessageDigest.getInstance(hashName);
		else
			throw new Exception("wrong hash name\n");
		
		outlen = hash.getDigestLength()*8;
	}
	
	/**
	 * computes the digest of the given byte array.
	 * @param d  byte array to be hashed.
	 * @return byte array containing the hash digest for d.
	 */
	public byte[] compute(byte[] d)
	{
		return hash.digest(d);
	}
	
	/**
	 * 
	 * @return number of bytes in the hash digest.
	 */
	public int getOutlen()
	{
		return outlen;
	}
}
